package com.eascs.ticket.ticket.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketComment;
import com.eascs.ticket.ticket.entity.TicketCommentDelete;
import com.eascs.ticket.ticket.entity.TicketCommentUpdate;
import com.eascs.ticket.ticket.entity.TicketCommentView;

@Service
public class TicketCommentService extends BaseSevice {
	@Inject
	private TicketChangeLogService ticketChangeLogService;

	public List<Object[]> getTicketCommentByTicketId(String ticketId) {
		return this.proxyDao.findObjectsList("select id, comment_content,updator,update_date from ticket_comment where ticket_id=? and status<> ? order by create_date", ticketId, StatusConstant.STATUS_DEL);
	}

	public List<TicketCommentView> getTicketCommentViewListByTicketId(String ticketId) {
		return this.proxyDao.findEntitys(
				"select tc.id, tc.comment_content,tc.status,tc.creator,tc.create_date,tc.updator,tc.update_date,uc.name creator_name,uu.name updator_name from ticket_comment tc left join user uc on uc.code=tc.creator  left join user uu on uu.code=tc.updator  where tc.ticket_id=? and tc.status<> ? order by tc.create_date",
				TicketCommentView.class, ticketId, StatusConstant.STATUS_DEL);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketComment(TicketComment ticketComment) {
		this.proxyDao.insert(ticketComment);
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketComment.getTicketId(), "增加备注", ticketComment);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void updateTicketComment(TicketCommentUpdate ticketCommentUpdate) {
		this.proxyDao.updateById(ticketCommentUpdate);
		TicketComment ticketComment = this.getTicketCommentById(ticketCommentUpdate.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketComment.getTicketId(), "修改备注", ticketCommentUpdate);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketComment(TicketCommentDelete ticketCommentDelete) {
		TicketComment ticketComment = this.getTicketCommentById(ticketCommentDelete.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketComment.getTicketId(), "删除备注", ticketCommentDelete);
		this.proxyDao.updateById(ticketCommentDelete);
	}

	public TicketComment getTicketCommentById(String id) {
		return this.proxyDao.getById(TicketComment.class, id);
	}
}
